package com.example.JavaWebApi.controller.systemController;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.JavaWebApi.core.apiinterceptors.Enum.RoleEnum;
import com.example.JavaWebApi.core.apiinterceptors.RequestHeaderContext;
import com.example.JavaWebApi.core.apiinterceptors.annotation.AllowAnonymous;
import com.example.JavaWebApi.core.apiinterceptors.annotation.Authorize;
import com.example.JavaWebApi.model.entity.systemEntity.Menuinfo;
import com.example.JavaWebApi.model.entity.systemEntity.Userinfo;
import com.example.JavaWebApi.service.systemService.IMenuinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  前端控制器
 * @author 杨泽
 * @since 2022-06-15
 */
@RestController
@RequestMapping("/api/menuinfo")
@Api(tags ="")
@Authorize(roles = {RoleEnum.SuperAdmin})
public class MenuinfoController {
 
    @Autowired
    public IMenuinfoService MenuinfoService;
    @GetMapping("GetMenuTree")
    @ApiOperation("获取菜单树")
    @Authorize(roles = {RoleEnum.SuperAdmin, RoleEnum.Admin})
    public List<Menuinfo> getMenuTree(){
        return MenuinfoService.getMenuTree();
    }
    @GetMapping("GetLazyList")
    @ApiOperation("获取懒加载菜单树")
    @Authorize(roles = {RoleEnum.SuperAdmin, RoleEnum.Admin})
    public IPage<Menuinfo> getLazyList(Integer parentId, int page, int limit){
        return MenuinfoService.getLazyList(parentId,page,limit);
    }
    @PostMapping("Create")
    @ApiOperation("创建菜单节点")
    public boolean create(@RequestBody Menuinfo menuinfo){
        return MenuinfoService.create(menuinfo);
    }
    @PostMapping("Update")
    @ApiOperation("更新菜单节点")
    public boolean update(@RequestBody Menuinfo menuinfo){
        return MenuinfoService.update(menuinfo);
    }
    @GetMapping("Delete")
    @ApiOperation("删除菜单节点")
    public boolean delete(Integer id){
        return MenuinfoService.delete(id);
    }
    @Authorize(roles = {RoleEnum.SuperAdmin, RoleEnum.Admin})
    @GetMapping("GetMenuByRoleId")
    @ApiOperation("根据角色id获取已经授权的菜单id列表")
    public List<Integer> getMenuByRoleId(Integer roleId){
        return MenuinfoService.getMenuByRoleId(roleId);
    }
    //@Authorize(roles = {RoleEnum.SuperAdmin, RoleEnum.Admin})
    @AllowAnonymous
    @GetMapping("GetMenuByUser")
    @ApiOperation("根据角色id获取菜单")
    public List<Menuinfo> getMenuByUser(){
//        Userinfo userinfo=RequestHeaderContext.getUserinfo();
//        return MenuinfoService.getMenuByUserId(userinfo.getUserRole().equals(RoleEnum.SuperAdmin.toString())?-9999:userinfo.getID());
        return MenuinfoService.getMenuByUserId(-9999);
    }
}
